﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.Hosting;
using System.Windows.Forms;
using fmtx.Utility;
using com.fmtx.IDal;
using com.fmtx.Model;
using fmtx.Framework.DALFactory;
using System.Web;

namespace CollectArticle
{
    class Program
    {
        private static readonly string mainpath = System.IO.Path.GetDirectoryName(@HostingEnvironment.ApplicationPhysicalPath) ?? Application.StartupPath;
        private static readonly string configPath = string.Format("{0}/{1}", mainpath, ConfigurationManager.AppSettings["Site.config"]).Replace("//", "/");
        private static Configuration Config
        {
            get
            {

                Configuration conf = new Configuration(configPath);
                return conf;
            }
        }
        static void Main(string[] args)
        {
            IArticle article = DataAccess<IArticle>.GetObject();
            foreach (var item in Config.Sites)
            {
                if (item.WebSite == "elong")
                {
                    DownHtml dh = new DownHtml();
                    var list = dh.ChaiTag(dh.DownFile(item.Url, "div", item.Ltype, item.Lval, item.Encode), item.Tag);
                    foreach (var s in list)
                    {
                        try
                        {
                            var al = dh.ChaiTag(s, "a");
                            var href = al[0].GetProperty("a","href")[0];
                            var title = al[0].RemoveHtml();
                            var area = al[1].RemoveHtml();
                            var html = dh.DownFile(href,"div", item.Dtype, item.Dval, item.Encode);

                            StringBuilder values = new StringBuilder();
                            values.Append("Title=");
                            values.Append(title);
                            values.Append("&Content=");
                            values.Append(HttpUtility.UrlEncode(html));
                            values.Append("&Uid=");
                            values.Append(item.Uid);
                            values.Append("&PlaceTag=");
                            values.Append(area);
                            values.Append("&TypeId=");
                            values.Append(item.Type);
                            values.Append("&Source=");
                            values.Append(HttpUtility.UrlEncode(href));
                            var res = fmtx.Utility.WebHelper.GetHtml("http://lvyougl.com/Write/Article/AppInsert", values.ToString(), 120, Encoding.UTF8);
                            Console.Write("id="+res);
                            Console.Write("\r\n");
                        }
                        catch (Exception ex)
                        {
                            Console.Write("id=" + ex);
                            Console.Write("\r\n");
                            fmtx.Framework.Log.Error(ex);
                        }
                    }
                }
                else if (item.WebSite == "xinxin")
                {
                    DownHtml dh = new DownHtml();
                    var list = dh.ChaiTag(dh.DownFile(item.Url, "div", item.Ltype, item.Lval, item.Encode), item.Tag);
                    foreach (var s in list)
                    {
                        try
                        {
                            var al = dh.ChaiTag(s, "a");
                            string href = "";
                            string title = "";
                            string area = "";
                            string html = "";
                            if (al.Count == 1)
                            {
                                href = al[0].GetProperty("a", "href")[0];
                                title = al[0].RemoveHtml();
                                html = dh.DownFile(href, "div", item.Dtype, item.Dval, item.Encode);
                            }
                            else
                            {
                                href = al[1].GetProperty("a", "href")[0];
                                title = al[1].RemoveHtml();
                                area = al[0].RemoveHtml();
                                html = dh.DownFile(href, "div", item.Dtype, item.Dval, item.Encode);
                            }

                            StringBuilder values = new StringBuilder();
                            values.Append("Title=");
                            values.Append(title);
                            values.Append("&Content=");
                            values.Append(HttpUtility.UrlEncode(html));
                            values.Append("&Uid=");
                            values.Append(item.Uid);
                            values.Append("&PlaceTag=");
                            values.Append(area);
                            values.Append("&TypeId=");
                            values.Append(item.Type);
                            values.Append("&Source=");
                            values.Append(HttpUtility.UrlEncode(href));
                            var res = fmtx.Utility.WebHelper.GetHtml("http://lvyougl.com/Write/Article/AppInsert", values.ToString(), 120, Encoding.UTF8);
                            Console.Write("id=" + res);
                            Console.Write("\r\n");
                        }
                        catch (Exception ex)
                        {
                            Console.Write("id=" + ex);
                            Console.Write("\r\n");
                            fmtx.Framework.Log.Error(ex);
                        }
                    }
                }
                Console.Write("完毕！");
            }
        }
    }
}
